package org.example;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class Main {
	public static void main(String[] args) {
		Class clazz = Message.class;
		Map<String, Integer> map = enumToMap(clazz, "name", "code");
		System.out.println(map);
	}

	private static Map<String, Integer> enumToMap(Class clazz, String keyFieldName, String valueFieldName) {
		Map<String, Integer> result = new HashMap<String, Integer>();
		Object[] enumConstants = clazz.getEnumConstants();
		try {
			Field keyField = clazz.getDeclaredField(keyFieldName);
			Field valueField = clazz.getDeclaredField(valueFieldName);
			keyField.setAccessible(true);
			valueField.setAccessible(true);
			for (Object enumConstant : enumConstants) {
				Object keyObj = keyField.get(enumConstant);
				String key = String.valueOf(keyObj);
				Object valueObj = valueField.get(enumConstant);
				Integer value = (Integer) valueObj;
				result.put(key, value);
			}
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return result;
	}

}
